#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

// includes, project
#include <cutil.h>

// includes, kernels

////////////////////////////////////////////////////////////////////////////////
// declarations, forward

extern "C"

int ReadFile(Matrix* M, char* file_name);


////////////////////////////////////////////////////////////////////////////////
// Program main
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char** argv) {

	Matrix  S;
	Matrix  Kf;
	Matrix  Kr;
	Matrix  m;
	
	
	srand(2012);
	
	if(argc = 6 ) 
	{
		// Allocate and read in matrices from disk
		int* params = NULL; 
		unsigned int data_read = 0;
		cutReadFilei(argv[1], &params, &data_read, true);
		if(data_read != 2){
			printf("Error reading parameter file\n");
			cutFree(params);
			return 1;
		}

		S  = AllocateMatrix(params[0], params[1]);
		Kf  = AllocateMatrix(params[1], 1);		
		Kr  = AllocateMatrix(params[1], 1);
		m  = AllocateMatrix(params[0], 1);
		cutFree(params);
		(void)ReadFile(&S, argv[2]);
		(void)ReadFile(&Kf, argv[3]);
		(void)ReadFile(&Kr, argv[4]);
		(void)ReadFile(&m, argv[5]);
	}
	else
	{
		
	       printf("unable reading parameter file\n");
			
	}

	

        
   
	return 0;
}

Matrix AllocateMatrix(int height, int width)
{
    Matrix M;
    M.width = M.pitch = width;
    M.height = height;
    int size = M.width * M.height;
    M.elements = NULL;
     
		
	M.elements = (float*) malloc(size*sizeof(float));

	for(unsigned int i = 0; i < M.height * M.width; i++)
	{
		if(init == 0) 		
			M.elements[i] = 0.0f;		
	}
    return M;
}	


int ReadFile(Matrix* M, char* file_name)
{
	unsigned int data_read = M->height * M->width;
	cutReadFilef(file_name, &(M->elements), &data_read, true);
	return data_read;
}

